package com.sdkj.seckill.rocketmqTest.primaryMessage;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Description 发送单向消息
 * @Author Jiang
 * @Date 2022/01/18 19:05
 * @Version 1.0.0
 */
//这种方式主要用在不特别关心发送结果的场景，例如日志发送
public class OnewayProducer {
    public static void main(String[] args) throws Exception {
        //实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        // 设置NameServer的地址
        producer.setNamesrvAddr("47.94.172.125:9876");
        //启动Producer实例
        producer.start();
        for (int i = 0; i < 100; i++) {
            Message msg = new Message("TopicTest", "TagA",
                    ("Hello RocketMQ "+i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //发送单向消息，没有任何返回结果
            producer.sendOneway(msg);
        }
        //如果不再发送消息，关闭生产者实例
        producer.shutdown();
    }
}
